package com.gxa.p2p.controller;

import com.gxa.p2p.common.JsonResult;
import com.gxa.p2p.pojo.Borrowuser;
import com.gxa.p2p.pojo.Investrecord;
import com.gxa.p2p.common.exception.MyException;
import com.gxa.p2p.pojo.Mark;
import com.gxa.p2p.pojo.Markclass;
import com.gxa.p2p.pojo.User;
import com.gxa.p2p.service.MarkService;
import com.gxa.p2p.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.*;

/**
 * @author ：looker
 * @date ：Created in 2021/10/25 19:19
 */
@CrossOrigin
@RestController
@RequestMapping("/mark")
@Component
//@Transactional
@Api(value = "标的操作的接口")
public class MarkController {

    @Autowired
    MarkService markService = null;

    /**
     * 接口13
     * 廖泓宇
     * @return 进行中标的管理的查询
     */
    @RequestMapping(value = "/findAllLikeByPageZB",method = RequestMethod.POST)
    @ApiOperation("进行中标的管理接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "legalName",value = "借款方名字",required = false,dataType = "Sting"),
            @ApiImplicitParam(name = "phone",value = "借款方手机号",required = false,dataType = "String"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "记录数",required = true,dataType = "int")
    })
    public Map findAllLikeByPageZB(@RequestParam("legalName") String legalName,@RequestParam("phone") String phone,@RequestParam("page") Integer page,@RequestParam("size") Integer size){
        System.out.println("legalName:"+legalName);
        System.out.println("phone:"+phone);
        List<Map> allLikeByPageZB = markService.findAllLikeByPageZB(legalName, phone, page, size);
        System.out.println("ZB:"+allLikeByPageZB);
        timeToStand(allLikeByPageZB);
        Integer count = markService.findAllLikeByPageZBCount(legalName, phone);
        System.out.println("count:"+count);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("count",count);
        System.out.println("DataMap:"+jsonResult.getDataMap());
        jsonResult.setData(allLikeByPageZB);
        return jsonResult.getDataMap();
    }

    /**
     * 接口14
     * 廖泓宇
     * @return 对标进行下架
     */
    @RequestMapping(value = "/undercarriageMark",method = RequestMethod.POST)
    @ApiOperation("标的下架按钮")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "remarks3",value = "下架备注",required = true,dataType = "String")

    })
    public Map undercarriageMark(@RequestParam("id") Long id,@RequestParam("remarks") String remarks){
        markService.ceMark(id,remarks);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    /**
     * 接口15
     * 廖泓宇
     * @return 满标复审包含模糊的全查询接口（返回字段不同）
     */
    @RequestMapping(value = "/findAllLikeByPageFS",method = RequestMethod.POST)
    @ApiOperation("满标复审的全查询和模糊查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "legalName",value = "借款方名字",required = false,dataType = "String"),
            @ApiImplicitParam(name = "phone",value = "借款方手机号",required = false,dataType = "String"),
            @ApiImplicitParam(name = "progress",value = "进度",required = false,dataType = "Boolean"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "记录数",required = true,dataType = "int")

    })
    public Map findAllLikeByPageFS(@RequestParam("legalName") String legalName,@RequestParam("phone") String phone,@RequestParam("progress") Boolean progress,@RequestParam("page") Integer page,@RequestParam("size") Integer size){
        List<Map> allLikeByPageFS = markService.findAllLikeByPageFS(legalName, phone, progress, page, size);
        System.out.println("FS:"+allLikeByPageFS);
        timeToStand(allLikeByPageFS);
        Integer count = markService.findAllLikeByPageFSCount(legalName, phone, progress);
        System.out.println("count:"+count);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("count",count);
        jsonResult.setData(allLikeByPageFS);
        return jsonResult.getDataMap();
    }

    /**
     * 接口16
     * 廖泓宇
     * @return 标的复审接口
     */
    @RequestMapping(value = "/doubleExamMark",method = RequestMethod.POST)
    @ApiOperation("标的复审按钮")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "state",value = "复审是否通过",required = true,dataType = "Boolean"),
            @ApiImplicitParam(name = "remarks2",value = "复审备注",required = true,dataType = "String"),

    })
    public Map DoubleExamMark(@RequestParam("id") Long id,@RequestParam("state") Boolean state,@RequestParam("remarks2") String remarks2){
        markService.DoubleExamMark(id, state, remarks2);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    /**
     * 接口17
     * 廖泓宇
     * @return 借款标包含模糊的全查询接口（返回字段不同）
     */
    @RequestMapping(value = "/findAllLikeByPageAJ",method = RequestMethod.POST)
    @ApiOperation("借款标的全查询和模糊查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "legalName",value = "借款方名字",required = false,dataType = "Long"),
            @ApiImplicitParam(name = "phone",value = "借款方手机号",required = false,dataType = "String"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "记录数",required = true,dataType = "int"),
            @ApiImplicitParam(name = "state1",value = "状态1",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state2",value = "状态2",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state3",value = "状态3",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state4",value = "状态4",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state5",value = "状态5",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state6",value = "状态6",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state7",value = "状态7",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state8",value = "状态8",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state9",value = "状态9",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state10",value = "状态10",required = false,dataType = "String")


    })
    public Map findAllLikeByPageAJ(@RequestParam("legalName") String legalName, @RequestParam("phone") String phone,
                                   @RequestParam("page") Integer page,@RequestParam("size") Integer size,@RequestParam("state1") String state1,
                                   @RequestParam("state2") String state2,@RequestParam("state3") String state3,@RequestParam("state4") String state4,@RequestParam("state5") String state5,
                                   @RequestParam("state6") String state6,@RequestParam("state7") String state7,@RequestParam("state8") String state8,
                                   @RequestParam("state9") String state9,@RequestParam("state10") String state10
                                   ){
        List<Map> allLikeByPageAJ = markService.findAllLikeByPageAJ(legalName, phone, page, size,state1,state2,state3,state4,state5,state6,state7,state8,state9,state10);
        Integer count = markService.findAllLikeByPageAJCount(legalName, phone, state1, state2, state3, state4, state5, state6, state7, state8, state9, state10);
        timeToStand(allLikeByPageAJ);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("count",count);
        jsonResult.setData(allLikeByPageAJ);
        return jsonResult.getDataMap();
    }

    /**
     * 接口18
     * 廖泓宇
     */
    @RequestMapping(value = "/findAllLikeByPageTZ",method = RequestMethod.POST)
    @ApiOperation("投资记录表模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "借款方电话",required = false,dataType = "String"),
            @ApiImplicitParam(name = "name",value = "标名",required = false,dataType = "String"),
            @ApiImplicitParam(name = "from",value = "起始时间",required = false,dataType = "String"),
            @ApiImplicitParam(name = "to",value = "终止时间",required = false,dataType = "String"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "记录数",required = true,dataType = "int"),
            @ApiImplicitParam(name = "state1",value = "状态1",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state2",value = "状态2",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state3",value = "状态3",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state4",value = "状态4",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state5",value = "状态5",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state6",value = "状态6",required = false,dataType = "String"),

    })
    public Map findAllLikeByPageTZ(@RequestParam("phone") String phone, @RequestParam("name") String name, @RequestParam("from") String from,@RequestParam("to") String to,@RequestParam("page") Integer page,@RequestParam("size") Integer size, @RequestParam("state1") String state1, @RequestParam("state2") String state2, @RequestParam("state3") String state3, @RequestParam("state4") String state4, @RequestParam("state5") String state5, @RequestParam("state6") String state6){
        Timestamp from1 = null;  Timestamp to1 = null;
        if(!StringUtil.isEmpty(from)){
            from1 = Timestamp.valueOf(from);
        }
        if(!StringUtil.isEmpty(to)){
            to1 = Timestamp.valueOf(to);
        }
        List<Investrecord> allLikeByPageTZ = markService.findAllLikeByPageTZ(phone, name, from1, to1, page, size, state1, state2, state3, state4, state5, state6);
        System.out.println("TZ:"+allLikeByPageTZ);
        Integer count = markService.findAllLikeByPageTZCount(phone, name, from1, to1, state1, state2, state3, state4, state5, state6);
        System.out.println("phone:"+phone);
        System.out.println("name:"+name);
        System.out.println("count:"+count);
        System.out.println("from1:"+from1);
        System.out.println("to1:"+to1);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.put("count",count);
        jsonResult.setData(allLikeByPageTZ);
        return jsonResult.getDataMap();
    }


    /**
     * 中标修改
     * 廖泓宇
     */
    @RequestMapping(value = "/updateZB",method = RequestMethod.POST)
    @ApiOperation("标的修改接口")
    public Map updateZB(@RequestBody  Mark mark){
        System.out.println(mark.getShelfTime());
        System.out.println(mark.getSaleTime());
        markService.updateZB(mark);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    /**
     * 类别修改
     * 廖泓宇
     */
    @RequestMapping(value = "/updateMarkClass",method = RequestMethod.POST)
    @ApiOperation("标的类别修改接口")
    public Map updateZB(@RequestBody Markclass markclass){
        markService.updateMarkClass(markclass);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }


    /**
     * 接口4
     * 新增借款时使用   传递新增借款页面所有参数
     * 杨述波
     * @param mark
     * @return
     */
    @PostMapping("/submit")
    @ApiOperation("新增借贷方法")
    public Map byId(@RequestBody Mark mark) throws MyException {
        System.out.println("接收到的数据"+mark);
        markService.subMard(mark);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功!");
        return jsonResult.getDataMap();
    }


    /**
     * 接口6
     * 新标维护页面查询接口 不传参就是全查询，传参就是模糊
     * 杨述波
     * @param markName
     * @param debtor
     * @param phone
     * @param state
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/findAllLikeByPageXW")
    @ApiOperation("新标维护页面查询接口 不传参就是全查询，传参就是模糊")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "markName",value = "借款标名",required = false,dataType = "String"),
            @ApiImplicitParam(name = "debtor",value = "借款方名字",required = false,dataType = "String"),
            @ApiImplicitParam(name = "phone",value = "电话",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state",value = "状态",required = false,dataType = "String"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "记录数",required = false,dataType = "int")
    })
    public Map findAllLikeByPageXW(String markName,String debtor,String phone,String state,Integer page,Integer size){
        JsonResult jsonResult = new JsonResult();
        List<Map> allLikeByPageByXW = markService.findAllLikeByPageByXW(markName, debtor, phone, state, page, size);
        timeToStand(allLikeByPageByXW);
        Integer allLikeXWCount = markService.findAllLikeXWCount(markName, debtor, phone, state);
        jsonResult.setCode("200");
        jsonResult.put("count",allLikeXWCount);
        jsonResult.setData(allLikeByPageByXW);
        return jsonResult.getDataMap();
    }


    /**
     * list中map的时间格式转换
     * 杨述波
     * @param list
     */
    public void timeToStand(List<Map> list){
        for (int i = 0; i < list.size(); i++) {
            Map map = list.get(i);
            System.out.println(map);
            Set set = ((HashMap) map).keySet();
            Iterator iterator = set.iterator();
            while (iterator.hasNext()) {
                String key = iterator.next().toString();
                Object o = ((HashMap) map).get(key);
                if (o instanceof Timestamp) {
                    ((HashMap) map).put(key,o.toString());
                }
            }
        }
    }

    /**
     * 接口7
     * 通过id查询标的全部信息
     * 杨述波
     * @param id
     * @return
     */
    @PostMapping("/findById")
    @ApiOperation("通过Id查询标的全部信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "标的Id",required = true,dataType = "long"),
    })
    public Map findById(@RequestParam("id") Long id) throws MyException {
        JsonResult jsonResult = new JsonResult();

        Mark byId = markService.findById(id);
        Borrowuser borrowById = markService.findBorrowById(byId.getDid());
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        jsonResult.setData(byId);
        jsonResult.put("borrow",borrowById);
        return jsonResult.getDataMap();
    }

    /**
     * 接口8
     * 通过标id删除标
     * 杨述波
     * @return
     */
    @ApiOperation("通过Id删除标")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "标的Id",required = true,dataType = "long"),
    })
    @PostMapping("/deleteById")
    public Map deleteById(@RequestParam("id") Long id) throws MyException {
        JsonResult jsonResult = new JsonResult();
        markService.deleteById(id);
        jsonResult.setCode("200");
        jsonResult.setMsg("删除成功");
        return jsonResult.getDataMap();
    }


    /**
     * 接口9
     * 杨述波
     * @return
     */
    @PostMapping("/findAllLikeByPageJK")
    @ApiOperation("借款审核页面查询接口 不传参就是全查询，传参就是模糊")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "debtor",value = "debtor",required = false,dataType = "String"),
            @ApiImplicitParam(name = "phone",value = "电话",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state",value = "状态",required = false,dataType = "String"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "页面大小",required = true,dataType = "int")
    })
    public  Map findAllLikeByPageJK(String debtor,String phone,String state,Integer page,Integer size){
        JsonResult jsonResult = new JsonResult();
        List<Map> allLikeByPageJK = markService.findAllLikeByPageJK(debtor, phone, state, page, size);
        timeToStand(allLikeByPageJK);
        Integer allLikeJKCount = markService.findAllLikeJKCount(debtor, phone, state);
        jsonResult.setCode("200");
        jsonResult.put("count",allLikeJKCount);
        jsonResult.setData(allLikeByPageJK);
        return jsonResult.getDataMap();
    }


    /**
     * 补充接口
     * 展示保存标
     * @param mark
     * @return
     */
    @PostMapping("/temp")
    @ApiOperation("新增借款标页面  暂时保存")
    public Map tempMark(@RequestBody Mark mark) throws MyException {
        System.out.println(mark);
        markService.tempMard(mark);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }


    /**
     * 接口10
     * 杨述波
     * 借款审核接口
     * @param id
     * @param path
     * @param reason
     * @return
     */
    @PostMapping("/examineMark")
    @ApiOperation("借款审核页面  审核结果提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "审核的标的id",required = true,dataType = "long"),
            @ApiImplicitParam(name = "path",value = "是否通过",required = true,dataType = "boolean"),
            @ApiImplicitParam(name = "reason",value = "借款审核备注",required = true,dataType = "String")
    })
    public Map examineMark(Long id,Boolean path,String reason){
        JsonResult jsonResult = new JsonResult();
        markService.checkFirstById(id,path,reason);
        jsonResult.setCode("200");
        jsonResult.setMsg("初审成功");
        return jsonResult.getDataMap();
    }

    /**
     * 接口11
     * 杨述波
     * @return
     */
    @PostMapping("/findAllLikeByPageSJ")
    @ApiOperation("标的上架查询页面  无参全查，有参筛选")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "debtor",value = "借款人",required = false,dataType = "String"),
            @ApiImplicitParam(name = "phone",value = "电话",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state",value = "状态",required = false,dataType = "String"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "int"),
            @ApiImplicitParam(name = "size",value = "页面大小",required = true,dataType = "int")
    })
    public Map findAllLikeByPageSJ(String debtor,String phone,String state,Integer page,Integer size){
        JsonResult jsonResult = new JsonResult();
        List<Map> allLikeByPageSJ = markService.findAllLikeByPageSJ(debtor, phone, state, page, size);
        timeToStand(allLikeByPageSJ);
        Integer allLikeSJCount = markService.findAllLikeSJCount(debtor, phone, state);
        jsonResult.setCode("200");
        jsonResult.put("count",allLikeSJCount);
        jsonResult.setData(allLikeByPageSJ);
        return jsonResult.getDataMap();
    }


    /**
     * 接口12
     * 杨述波
     * @return
     */
    @PostMapping("/fillMark")
    @ApiOperation("标的上架维护页面--标信息填充接口")
    public Map fillMark(@RequestBody Mark mark){
        System.out.println(mark);
        markService.fillById(mark);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("成功");
        return jsonResult.getDataMap();
    }




}
